package csu.fun.spring_store.persistence;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import csu.fun.spring_store.domain.Account;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Repository
public interface AccountMapper extends BaseMapper<Account> {


    @Select("select password from signon where username = #{id}")
    String getpassward(String id);
    @Update("update signon set password =#{password} where username = #{id}")
    void reset(String password,String id);

//    @Results(value = {
//            @Result(column = "langpref",property = "lan"),
//            @Result(column = "mylistopt",property = "myl"),
//            @Result(column = "banneropt",property = "ban"),
//            @Result(column = "banneropt",property = "fav")})
//    @Select("select * from profile where userid = #{id}")

    @Update("update profile set langpref=#{lan}, favcategory=#{fav}, mylistopt=#{myl}, banneropt=#{ban} where userid=#{id} ")
    void reprofile(String lan,String fav,int myl,int ban, String id);

    //判断是否存在指定id的account（userid不区分大小写）
    default boolean isExistAccount(String userid) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", userid);
        return this.exists(queryWrapper);
    }

    //返回指定id的account
    default Account getAccountByUserid(String userid) {
        return this.selectById(userid);
    }

    //插入一个新account，返回实际操作的行数。account不为null且userid必须不在数据库中存在，否则不进行任何修改
    default int setNewAccount(Account account) {
        if (account == null) {
            return 0;
        }
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", account.getUserid());
        if (!this.exists(queryWrapper)) {
            return this.insert(account);
        }
        return 0;
    }

    //一次性更新整条记录，返回实际修改的行数。account不能为null且userid必须已经在数据库中存在，否则不进行任何修改
    default int setExistAccount(Account account) {
        if (account != null) {
            return this.updateById(account);
        }
        return 0;
    }

    /*
      单独修改数据库中的某个值，返回修改的行数。userid必须已经在数据库中存在，否则不进行任何修改
     */
    default int setEmailByUserid(String userid, String email) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setEmail(email);
            return this.updateById(account);
        }
        return 0;
    }
    default int setFirstnameByUserid(String userid, String firstname) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setFirstname(firstname);
            return this.updateById(account);
        }
        return 0;
    }
    default int setLastnameByUserid(String userid, String lastname) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setLastname(lastname);
            return this.updateById(account);
        }
        return 0;
    }
    default int setStatusByUserid(String userid, String status) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setStatus(status);
            return this.updateById(account);
        }
        return 0;
    }
    default int setAddr1ByUserid(String userid, String addr1) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setAddr1(addr1);
            return this.updateById(account);
        }
        return 0;
    }
    default int setAddr2ByUserid(String userid, String addr2) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setAddr2(addr2);
            return this.updateById(account);
        }
        return 0;
    }

    default int setCityByUserid(String userid, String city) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setCity(city);
            return this.updateById(account);
        }
        return 0;
    }
    default int setStateByUserid(String userid, String state) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setState(state);
            return this.updateById(account);
        }
        return 0;
    }
    default int setZipByUserid(String userid, String zip) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setZip(zip);
            return this.updateById(account);
        }
        return 0;
    }
    default int setCountryByUserid(String userid, String country) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setCountry(country);
            return this.updateById(account);
        }
        return 0;
    }
    default int setPhoneByUserid(String userid, String phone) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setPhone(phone);
            return this.updateById(account);
        }
        return 0;
    }

    //删除指定id的account，返回实际修改的行数
    default int removeAccountByUserid(String userid) {
        return this.deleteById(userid);
    }

}
